package com.stu.aspectj2;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspect {
	//定义切入点表达式
	@Pointcut("execution(* com.stu.aspectj2.*.*(..))")
	private void myPointCut() {}
	
	//前置通知
	@Before("myPointCut()")
	public void before(JoinPoint joinpoint) {
		System.out.println("前置通知：模拟执行权限检查...,");
		System.out.println("目标类通知是："+joinpoint.getTarget());
		System.out.println(",被植入增强处理的目标方法为："+joinpoint.getSignature().getName());
	}
	//后置通知
	@AfterReturning(value="myPointCut()")
	public void afterReturning(JoinPoint joinPoint) {
		System.out.println("后置通知：模拟记录日志...,");
		System.out.println("被植入增强处理的目标方法为："+joinPoint.getSignature().getName());
	}
	//环绕通知
	@Around("myPointCut()")
	public Object around(ProceedingJoinPoint pro) throws Throwable {
		//开始
		System.out.println("环绕开始：执行目标方法之前，模拟开启事务...");
		Object obj=pro.proceed();
		System.out.println("环绕结束：执行目标方法之后，模拟关闭事物...");
		return obj;
	}
	//异常通知
		@AfterThrowing(value="myPointCut()",throwing="e")
		public void thorwException(JoinPoint joinpoint,Throwable e) {
			System.out.println("异常通知："+"出错了"+e.getMessage());
		}
	@After("myPointCut()")
	public void after() {
		System.out.println("最终通知：模拟方法结束后的释放资源...");
	}
}
